<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Classification extends Controller
{
    public function _initialize()
    {
        if (Session::get('user_name') && Session::get('pass_word')) {
            $user_info = Db::table('super_user')
                ->where([
                    'user_name' => Session::get('user_name'),
                    'password' => Session::get('pass_word'),
                ])
                ->find();
            if (!$user_info) {
                $this->error('状态失效');
            }
        } else {
            $this->error('请登录');
        }
    }
    public function index()
    {
        $list = Db::table('class')
            ->where('start', 'eq', 0)
            ->select();
        return $this->fetch(null, ['list' => $list, 'DataTime' => date('Y-m-d H:i:s')]);
    }
    public function edit()
    {
        $start = Db::table('class')
            ->where(['id' => $_POST['id']])
            ->update(['name' => $_POST['name']]);
        if ($start) {
            $this->success('true');
        } else {
            $this->error('error');
        }
    }
    public function delete()
    {
        $start = Db::table('class')
            ->where(['id' => $_POST['id']])
            ->update(['start' => 1]);
        if ($start) {
            $this->success('true');
        } else {
            $this->error('error');
        }
    }
    public function deletelist()
    {
        $list = Db::table('class')
            ->where('start', 'eq', 1)
            ->select();
        return $this->fetch(null, ['list' => $list, 'DataTime' => date('Y-m-d H:i:s')]);
    }
    public function deletelist_recovery()
    {
        $start = Db::table('class')
            ->where(['id' => $_POST['id']])
            ->update(['start' => 0]);
        if ($start) {
            $this->success('true');
        } else {
            $this->error('error');
        }
    }
    public function deletelist_delete()
    {
        $start = Db::table('class')
            ->where(['id' => $_POST['id']])
            ->delete();
        if ($start) {
            $this->success('true');
        } else {
            $this->error('error');
        }
    }
    public function Add_class()
    {
        $name = input('name', null);
        if ($name) {
            $start = Db::table('class')
                ->insertGetId([
                    'name' => $name,
                    'add_time' => date('Y-m-d H:i:s'),
                    'start' => 0,
                ]);
            if ($start) {
                $this->success(['date' => date('Y-m-d H:i:s'), 'id' => $start]);
            } else {
                $this->error('error');
            }
        }
    }
}
